import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/entity/entities/lf_photo_entity.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/lf_text_style.dart';
import 'package:largefriends/utils/lf_scroll_behavior.dart';
import 'package:largefriends/widgets/preload_page_view.dart';

import '../../../../res/lf_fonts.dart';
import '../../../../widgets/custom_cache_image.dart';
import 'lf_manage_photo_gallery_logic.dart';

class LfManagePhotoGalleryPage extends StatefulWidget {
  const LfManagePhotoGalleryPage({super.key});

  @override
  State<LfManagePhotoGalleryPage> createState() =>
      _LfManagePhotoGalleryPageState();
}

class _LfManagePhotoGalleryPageState extends State<LfManagePhotoGalleryPage> {
  final logic = Get.put(LfManagePhotoGalleryLogic());

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.black26,
            leading: InkWell(
              onTap: () {
                Get.back();
              },
              child: Icon(
                Icons.arrow_back,
                color: colorFEFEFE,
              ),
            ),
            automaticallyImplyLeading: false,
            centerTitle: false,
            title: GetBuilder<LfManagePhotoGalleryLogic>(
              builder: (logic) {
                return Obx(() {
                  bool isPrivate =
                      logic.photos[logic.currentIndex.value].type ==
                          photoTypePrivate;
                  return Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Text(
                        "${logic.currentIndex.value + 1}/${logic.photos.length}",
                        style: lfTextStyle(
                          sp: 16.sp,
                          fontFamily: fontBold,
                          color: colorFEFEFE,
                        ),
                      ),
                      5.w.sbw,
                      Opacity(
                        opacity: isPrivate ? 1 : 0,
                        child: Icon(
                          Icons.lock_rounded,
                          size: 20.r,
                          color: colorFEFEFE,
                        ),
                      )
                    ],
                  );
                });
              },
            ),
            actions: [
              InkWell(
                child: Padding(
                  padding: EdgeInsets.only(right: 16.w),
                  child: Icon(
                    Icons.more_horiz,
                    color: colorFEFEFE,
                  ),
                ),
                onTap: () {
                  logic.showMoreAction();
                },
              )
            ],
          ),
          extendBodyBehindAppBar: true,
          body: GetBuilder<LfManagePhotoGalleryLogic>(
            builder: (logic) {
              return ScrollConfiguration(
                behavior: LfScrollBehavior(),
                child: PreloadPageView.builder(
                  itemBuilder: (context, index) {
                    return InkWell(
                      child: Container(
                          width: ScreenUtil().screenWidth,
                          height: ScreenUtil().screenHeight,
                          alignment: Alignment.center,
                          color: Colors.black,
                          child: CustomCacheImage(
                            imageUrl: logic.photos[index].url,
                            extendedImageMode: ExtendedImageMode.gesture,
                          )),
                      onTap: () {
                        Get.back();
                      },
                    );
                  },
                  itemCount: logic.photos.length,
                  controller: logic.controller,
                  preloadPagesCount: logic.photos.length,
                  onPageChanged: (value) {
                    logic.currentIndex.value = value;
                  },
                ),
              );
            },
          ),
        ),
        onWillPop: () async {
          Get.back();
          return false;
        });
  }
}
